<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script src="./Promise.js"></script>

  <script>
    const p1 = Promise.resolveDelay(2, 1000)
    const p2 = Promise.rejectDelay(3, 2000)
    console.log(p1, p2)

  </script>

  <!-- <script>
    const p1 = Promise.resolve(2)
    const p2 = Promise.resolve(new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve(3)
      }, 1000)
    }))
    const p3 = Promise.reject(4)

    const p4 = Promise.all([p2, p1, {}])
    // const p4 = Promise.all([p1, p2, p3, {}])
    p4.then(
      values => {
        console.log('onResolved', values)
      },
      reason => {
        console.log('onRejected', reason)
      }
    )

  </script> -->


  <!-- <script>
    const p1 = Promise.resolve(3)
    console.log(p1)
    const p2 = Promise.resolve(new Promise((resolve, reject) => {
      reject(5)
    }))
    const p3 = Promise.resolve(Promise.resolve(6))

    const p4 = Promise.reject(7)
    console.log('p1', p1)
    console.log('p2', p2)
    console.log('p3', p3)
    console.log('p4', p4)
  </script> -->




  <!-- <script>
    const p = new Promise((resolve, reject) => {
      console.log('executor')
      // 启动异步任务
      setTimeout(() => {
        // 如果成功了, 调用resolve
        // resolve(2)  // promise对象变为成功了, 此时内部有待执行的回调函数 ==> 执行回调函数
        // 如果失败了, 调用reject
        reject(3)
        resolve(2) // 这一次调用应该无效

        console.log('reject()之后')
      }, 2000)
    })
    console.log(p)
    p.then(  // 如果当前promise对象还是pending, 内部得先将回调保存起来
      value => {
        console.log('onResolved', value)
      },
      reason => {
        console.log('onRejected', reason)
      }
    )
    p.then(
      value => {
        console.log('onResolved2', value)
      },
      reason => {
        console.log('onRejected2', reason)
      }
    )

    /* 
    [
      [value => {}, reason => {}],
      [value => {}, reason => {}]
    ]

    [
      {onResolved: value => {}, onRejected: reson => {}}
      {onResolved: value => {}, onRejected: reson => {}}
    ]
    */
  </script> -->

  <!-- <script>
    new Promise((resolve, reject) => {
      // setTimeout(() => {
      //   resolve(2)
      // }, 1000)
      // 立即调用resolve
      resolve(2)
      // reject(3)
    }).then(
      // value => {
      //   console.log('onResolved1', value)

      //   // 如果回调函数返回一个成功的promise 
      //     // ==> then返回的promise就是成功的, 且value是回调返回的promise的value
      //   // throw 4
      //   // return new Promise((resolve, reject) => {
      //   //   reject(6)
      //   // })
      //   // return new Promise((resolve, reject) => {
      //   //   resolve(5)
      //   // })
      //   // return 3
      // },

      // reason => {
      //   console.log('onRejected1', reason)
      // }
    ).then(
      value => {
        console.log('onResolved2', value)
        // return 5
        throw 6
      },

      reason => {
        console.log('onRejected2', reason)
      }
    ).catch(reason => {
      console.log('catch onRejected3', reason)
    }).then(
      value => {
        console.log('onResolved4', value)
      },

      reason => {
        console.log('onRejected4', reason)
      }
    )


  </script> -->
</body>
</html>